package cn.edu.nwafu.hjpg.controller;

import cn.edu.nwafu.hjpg.common.HttpResult;
import cn.edu.nwafu.hjpg.entity.Jkwhb;
import cn.edu.nwafu.hjpg.entity.Lhsxb;
import cn.edu.nwafu.hjpg.service.JkwhbService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/jkwhb")
public class JkwhbController {
    final JkwhbService jkwhbService;

    public JkwhbController(JkwhbService jkwhbService) {this.jkwhbService = jkwhbService;}

    @GetMapping(value = "/")
    @ResponseBody
    public HttpResult get(@RequestParam(value = "labelName",defaultValue = "",required = false) String name,
                          @RequestParam(value = "elementId",defaultValue = "",required = false) String id,
                          @RequestParam(value = "flag",defaultValue = "",required = false) String flag){
        System.out.println(flag);
        return HttpResult.ok(jkwhbService.findByIdAndFlagInJkwhb(id,name,flag));
    }
    @PostMapping(value = "/update")
    @ResponseBody
    public HttpResult update(@RequestBody Jkwhb jkwhb) {
        return HttpResult.ok(jkwhbService.updateJkwhb(jkwhb));
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public HttpResult add(@RequestBody(required = false) Jkwhb jkwhb,@RequestParam(value = "labelName",defaultValue = "",required = false) String name)
    {
        switch(name){
        case "健康危害分类":
            return HttpResult.ok(jkwhbService.addJkwhfl(jkwhb));
        case "经口急性毒性":
            return HttpResult.ok(jkwhbService.addJkjxdx(jkwhb));
        case "经皮急性毒性":
            return HttpResult.ok(jkwhbService.addJpjxdx(jkwhb));
        case "吸入急性毒性":
            return HttpResult.ok(jkwhbService.addXrjxdx(jkwhb));
        case "慢性经口毒性":
            return HttpResult.ok(jkwhbService.addMxjkdx(jkwhb));
        case "慢性经皮毒性":
            return HttpResult.ok(jkwhbService.addMxjpdx(jkwhb));
        case "慢性吸入毒性":
            return HttpResult.ok(jkwhbService.addMxxrdx(jkwhb));
        case "致癌性":
            return HttpResult.ok(jkwhbService.addZax(jkwhb));
        case "RfC":
            return HttpResult.ok(jkwhbService.addRfc(jkwhb));
        case "RfD":
            return HttpResult.ok(jkwhbService.addRfd(jkwhb));
        case "经口SF":
            return HttpResult.ok(jkwhbService.addJksf(jkwhb));
        case "吸入IUR":
            return HttpResult.ok(jkwhbService.addXriur(jkwhb));
        case "饮用水因子":
            return HttpResult.ok(jkwhbService.addYysyz(jkwhb));
        case "致突变性":
            return HttpResult.ok(jkwhbService.addZtbx(jkwhb));
        case "生殖毒性":
            return HttpResult.ok(jkwhbService.addSzdx(jkwhb));
        case "神经毒性":
            return HttpResult.ok(jkwhbService.addSjdx(jkwhb));
        case "皮肤致敏":
                return HttpResult.ok(jkwhbService.addPfzm(jkwhb));
        case "呼吸致敏":
                return HttpResult.ok(jkwhbService.addHxzm(jkwhb));
        case "腐蚀/刺激性":
                return HttpResult.ok(jkwhbService.addFscjx(jkwhb));
        case "流行病学信息":
                return HttpResult.ok(jkwhbService.addLxbxxx(jkwhb));
        case "其他毒理学信息":
                return HttpResult.ok(jkwhbService.addQtdlxxx(jkwhb));
        case "健康毒代动力学":
                return HttpResult.ok(jkwhbService.addJkdddlx(jkwhb));
        case "健康危害信息备注":
                return HttpResult.ok(jkwhbService.addJkwhxxbz(jkwhb));
        default:
            return null;}
    }

    @GetMapping(value = "/addRecord")
    @ResponseBody
    public HttpResult queryJkwhbByPage(@RequestParam(value = "flag",defaultValue = "")String flag,
                                       @RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber,
                                       @RequestParam(value = "pageSize",defaultValue = "20") int pageSize){

        Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
        return HttpResult.ok(jkwhbService.findAddRecordJkwhb(flag,pageable));
    }
    @GetMapping(value = "/find")
    @ResponseBody
    public HttpResult findJkwhbByPage(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                      @RequestParam(value = "pageSize", defaultValue = "7") int pageSize,
                                      @RequestParam(value = "attribute", defaultValue = "") String attribute,
                                      @RequestParam(value = "flag", defaultValue = "1") int flag,
                                      @RequestParam(value = "id",defaultValue = "",required = false) String id){

        Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
        return HttpResult.ok(jkwhbService.findByPageJkwhb(id,attribute,flag,pageable));
    }
    @PostMapping(value = "/addupdateFlag")
    @ResponseBody
    public HttpResult addupdateFlag(@RequestBody Jkwhb jkwhb){
        return HttpResult.ok(jkwhbService.addupdateFlag(jkwhb));
    }
    @DeleteMapping("/{id}")
    @ResponseBody
    public HttpResult deladdRecordjkwhb(Jkwhb jkwhb){
        jkwhbService.deladdRecordjkwhb(jkwhb);
        return HttpResult.ok();
    }
    @PostMapping(value = "/import")
    @ResponseBody
    public HttpResult addExcelInJkwhb(@RequestBody List<Jkwhb> jkwhbList) {
        for (int i = 0 ; i <jkwhbList.size() ; i ++ ){
            jkwhbList.get(i).setFlag("1");

            jkwhbService.addElement(jkwhbList.get(i));
        }
        return HttpResult.ok();
    }
    @GetMapping(value = "/all")
    @ResponseBody
    public HttpResult findAll(@RequestParam(value = "elementId",defaultValue = "",required = false) String id,
                              @RequestParam(value = "flag",defaultValue = "",required = false) String flag) {
        System.out.println("**********************");
        return HttpResult.ok(jkwhbService.findAllByBaseinfoWzbmAndFlag(id,flag));
    }
    @GetMapping(value = "/export")
    @ResponseBody
    public HttpResult get() {
        return HttpResult.ok(jkwhbService.findAll());
    }


}
